package com.leetcode.demo3Solution.java;

import sun.reflect.annotation.AnnotationSupport;

//最小子序和
public class demo53 {

    public static void main(String[] args) {
        int[] nums={-2,1,-3,4,-1,2,1,-5,4};
        System.out.println("最小子序和:"+ maxSubArray(nums));
    }
    public static  int maxSubArray(int[] nums) {
       if(nums.length==1)  return nums[0];
       int sum=0;
       int ans=nums[0];
       if(nums.length>1){
           for(int i=0;i<nums.length;i++){
               if(sum>0){
                   sum+=nums[i];
               }
               else {
                   sum=nums[i];
               }
               ans=Math.max(sum,ans);
           }
       }
       return ans;
    }
}

